#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>>dp(n, vector<int>(m));
        dp[0][0] = grid[0][0];
        for (int i = 1; i < m; i++)
        {
            dp[0][i] = grid[0][i] + dp[0][i - 1];

        }
        for (int j = 1; j < n; j++)
        {
            dp[j][0] = dp[j - 1][0] + grid[j][0];
        }
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++)
            {
                int lower = min(dp[i - 1][j], dp[i][j - 1]);
                dp[i][j] = lower + grid[i][j];
            }
        }
        return dp[n - 1][m - 1];
    }
};